package zisu.algorithm.algorithm.Map;

import zisu.algorithm.algorithm.BST.FileOperation;
import java.util.ArrayList;
import zisu.algorithm.algorithm.BST.Map.Map;

/**
 * @Author: 蔡翔
 * @Date: 2019/12/21 18:21
 * @Version 1.0
 *
 * 链表的映射类。
 */
public class LinkedListMap<K,V> implements Map<K,V> {
    //结点--内部类。
    private class Node{

        private K key;
        private V value;

        private Node next;

        public Node(K key, V value, Node next){
            this.key = key;
            this.value = value;
            this.next = next;
        }

        public Node(K key){
            this(key , null ,null);
        }

        public Node(K key,V value){
            this(key,value,null);
        }

        public Node(){
            this(null,null);
        }

        @Override
        public String toString() {
            return "Node{" +
                    "key = " + key +
                    ", value = " + value +
                    ", next=" + next +
                    '}';
        }
    }

    private Node dummyHead;
    private int size;

    // 要加public
    public LinkedListMap(){
        this.dummyHead = new Node();
        this.size = 0;
    }

    public int getSize() {
        return size;
    }

    @Override
    public boolean isEmpty() {
        return size==0;
    }

    public Node getNode(K key) {
        Node cur = dummyHead.next;
        while (cur != null){
            if(cur.key.equals(key)){
                return cur;
            }
            cur = cur.next;
        }
        return null;
    }

    @Override
    public boolean contains(K key) {
        return getNode(key) == null? false:true;
    }

    @Override
    public V get(K key) {
        Node node = getNode(key);
        if(node == null){
            return null;
        }
        return node.value;
    }

    @Override
    public void add(K key, V value) {
        Node node = getNode(key);
        if(node == null){
            dummyHead.next = new Node(key,value,dummyHead.next);
            size++;
        }else {
            node.value = value;
        }
    }

    @Override
    public void set(K key, V newValue) {
        Node node = getNode(key);
        // 如果set 的key 不存在那就重新new 一个
        if(node == null){
            add(key,newValue);
        }
        node.value = newValue;
    }

    @Override
    public V remove(K key) {
        Node preNode = dummyHead;
        while (preNode.next != null){
            if(preNode.next.key.equals(key)){
                break;
            }
            preNode = preNode.next;
        }
        if(preNode.next != null){
            Node delNode = preNode.next;
            preNode.next = delNode.next;
            delNode.next = null;
            size --;
            return delNode.value;
        }
        return null;
    }

    public static void main(String[] args) {
        System.out.println("pride-and-prejudice.txt");
        ArrayList<String> arrayList = new ArrayList<>();
        if(FileOperation.readFile("pride-and-prejudice.txt",arrayList)){
            System.out.println("Total word:"+arrayList.size());
        }
        LinkedListMap<String,Integer> linkedListMap = new LinkedListMap<>();
        for (String word:arrayList){
            if(linkedListMap.contains(word)){
               linkedListMap.set(word,linkedListMap.get(word)+1);
            }else {
                linkedListMap.add(word,1);
            }
        }
        System.out.println("Total different words :"+ linkedListMap.getSize());
        System.out.println("Frequence of pride :"+linkedListMap.get("pride"));
        System.out.println("Frequence of prejudice :"+linkedListMap.get("prejudice"));

    }
}